﻿using NPoco;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using YYBlog.Core.Extensions;

namespace YYBlog.Core.Entity
{
    [TableName("BlogPost")]
    [PrimaryKey("Id", AutoIncrement = true)]
    public class BlogPost
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string AuthorId { get; set; }
        public string AuthorDisplayName { get; set; }
        public string TitleSlug { get; set; }
        public string Content { get; set; }
        public PublishStatus Status { get; set; }
        public DateTime PubDate { get; set; }
        public string Tags { get; set; }
        public string MarkDown { get; set; }
        
        public string AuthorEmail { get; set; }

        [Ignore]
        public bool IsPublished
        {
            get { return PubDate <= DateTime.Now && Status == PublishStatus.Published; }
        }

        [Ignore]
        public string Description
        {
            get
            {
                if (string.IsNullOrWhiteSpace(Content))
                    return string.Empty;
                var des = Regex.Replace(Content, "<[^>]+>", string.Empty).Trim();
                var length = des.Length < 240 ? des.Length : 240;
                des = des.Substring(0, length) + " ...";
                return des;
            }
        }
        [Ignore]
        public string[] TagsArray
        {
            get
            {
                return Tags.Trim().Split(',').Select(s => s.Trim()).ToArray();
            }
        }

         
        public string GetLink()
        {
            return "/{0}/{1}".FormatWith(PubDate.ToString("yyyy/MM", CultureInfo.InvariantCulture), TitleSlug);
        }
    }

    public enum PublishStatus
    {
        Published,
        Draft
    }
}

